using System;
using System.Configuration;
using System.ServiceModel.Channels;
using System.ServiceModel.Configuration;
using Albatross.WCF.Extenions.ServiceModel.Channels;

namespace Albatross.WCF.Extenions.ServiceModel.Configuration
{
    public sealed class CompressionElement : BindingElementExtensionElement
    {
        private const string AlgorithmName = "algorithm";
        private const string LevelName = "level";
        private ConfigurationPropertyCollection properties;

        public override Type BindingElementType
        {
            get { return typeof (CompressionBindingElement); }
        }

        [ConfigurationProperty(LevelName, DefaultValue = CompressionLevel.Normal)]
        public CompressionLevel Level
        {
            get { return (CompressionLevel) base[LevelName]; }
            set { base[LevelName] = value; }
        }

        [ConfigurationProperty(AlgorithmName, DefaultValue = CompressionAlgorithm.GZip)]
        public CompressionAlgorithm Algorithm
        {
            get { return (CompressionAlgorithm) base[AlgorithmName]; }
            set { base[AlgorithmName] = value; }
        }

        protected override ConfigurationPropertyCollection Properties
        {
            get
            {
                if (properties == null)
                {
                    var propertys = new ConfigurationPropertyCollection
                                        {
                                            new ConfigurationProperty(LevelName, typeof (CompressionLevel),
                                                                      CompressionLevel.Normal, null, null,
                                                                      ConfigurationPropertyOptions.None),
                                            new ConfigurationProperty(AlgorithmName, typeof (CompressionAlgorithm),
                                                                      CompressionAlgorithm.GZip, null, null,
                                                                      ConfigurationPropertyOptions.None)
                                        };
                    properties = propertys;
                }
                return properties;
            }
        }

        protected override BindingElement CreateBindingElement()
        {
            var bindingElement = new CompressionBindingElement();
            ApplyConfiguration(bindingElement);
            return bindingElement;
        }

        public override void ApplyConfiguration(BindingElement bindingElement)
        {
            base.ApplyConfiguration(bindingElement);
            var element = (CompressionBindingElement) bindingElement;
            element.Level = Level;
            element.Algorithm = Algorithm;
        }

        public override void CopyFrom(ServiceModelExtensionElement from)
        {
            base.CopyFrom(from);
            var element = (CompressionElement) from;
            Level = element.Level;
            Algorithm = element.Algorithm;
        }

        protected override void InitializeFrom(BindingElement bindingElement)
        {
            base.InitializeFrom(bindingElement);
            var element = (CompressionBindingElement) bindingElement;
            Level = element.Level;
            Algorithm = element.Algorithm;
        }
    }
}